import json
import os
import platform

import core
import data

class Config:
    def __init__(self, file:str) -> None:
        self.data = {}
        self.keys = []
        self.file = file

    def set(self, key:str, value) -> None:
        self.data[key] = value

    def get(self, key:str) -> None:
        return self.data[key]
    
    def save(self) -> None:
        with open(self.file, "w+",encoding="utf-8") as f:
            f.write(json.dumps(self.data))
    
    def load(self) -> None:
        try:
            with open(self.file,"r", encoding="utf-8") as f:
                self.data = json.load(f)
        except OSError as e:
            self.data = self.create()
            self.save()
    
    def create(self) -> dict:
        result = {}
        for key, default in self.keys:
            result[key] = default
        return result
    
    def add_key(self, key:str, default) -> None:
        self.keys.append((key,default))

@core.once
def get_config() -> Config:
    conf = Config(data.get_config_path())
    conf.add_key("git","")
    conf.add_key("python","")
    conf.add_key("mirror",None)

    return conf